import { insertElementBefore } from './public';

// 复制功能
function DownloadFunction(inputVal) {
  var aux = document.createElement('input');
  aux.setAttribute('value', inputVal);
  document.body.appendChild(aux);
  aux.select();
  document.execCommand('copy');
  document.body.removeChild(aux);
}

// 开头字母转大写
function startWithUppercase(s) {
  return s
    .toLowerCase()
    .split(/\s+/)
    .reduce(function (prev, item) {
      return (
        prev +
        (prev.trim() && ' ') +
        item.slice(0, 1).toUpperCase() +
        item.slice(1)
      );
    }, '');
}
// 开头字母转小写
function startWithLowercase(s) {
  return s
    .toLowerCase()
    .split(/\s+/)
    .reduce(function (prev, item) {
      return (
        prev +
        (prev.trim() && ' ') +
        item.slice(0, 1).toLowerCase() +
        item.slice(1)
      );
    }, '');
}

export function translateBaidu() {
  setTimeout(() => {
    let boxEl = document.createElement('div');
    let html = `
    <button id="translateBaidu1" class="boxue_translateGoogle_button">全大写(下划线)</button>
    <button id="translateBaidu2" class="boxue_translateGoogle_button">驼峰</button>
    <button id="translateBaidu5" class="boxue_translateGoogle_button">(A)驼峰</button>
    <button id="translateBaidu6" class="boxue_translateGoogle_button">(S)接口</button>
    <button id="translateBaidu7" class="boxue_translateGoogle_button">事件(H)</button>
    <button id="translateBaidu3" class="boxue_translateGoogle_button">下划线</button>
    <button id="translateBaidu4" class="boxue_translateGoogle_button">中划线</button>
  `;
    boxEl.style.padding = '0 0 20px 0';
    boxEl.setAttribute('id', 'fanyiBox');
    boxEl.innerHTML = html;
    insertElementBefore(
      boxEl,
      document.getElementsByClassName('translate-wrap')[0]
    );
    window.addEventListener('click', (e) => {
      let currentId = e.target.id;
      if (
        [
          'translateBaidu1',
          'translateBaidu2',
          'translateBaidu3',
          'translateBaidu4',
          'translateBaidu5',
          'translateBaidu6',
          'translateBaidu7',
        ].includes(currentId)
      ) {
        try {
          let val =
            document.getElementsByClassName('output-bd')[0].innerText || '';
          if (val === '') {
            console.log('');
          }
          if (currentId === 'translateBaidu1') {
            // 全大写(下划线)
            DownloadFunction(
              val
                .toUpperCase()
                .replace(/-/g, ' ')
                .replace(/\ /g, '_')
                .replace(/\\n/g, ' ')
            );
          } else if (currentId === 'translateBaidu2') {
            // 驼峰
            val = startWithUppercase(val); //转换开头字母大写
            val = val.replace(/\\n/g, ' ');
            val = val.replace(/-/g, ' ');
            val = val.replace(/\s+/g, ''); //去掉空格
            val = val.substring(0, 1).toLowerCase() + val.substring(1);
            val = val.replace(/,/g, '');
            val = val.replace(/\./g, '');
            DownloadFunction(val);
          } else if (currentId === 'translateBaidu5') {
            // 驼峰
            val = startWithUppercase(val); //转换开头字母大写
            val = val.replace(/\\n/g, ' ');
            val = val.replace(/-/g, ' ');
            val = val.replace(/\s+/g, ''); //去掉空格
            val = val.replace(/,/g, '');
            val = val.replace(/\./g, '');
            DownloadFunction(val);
          } else if (currentId === 'translateBaidu6') {
            val = startWithUppercase(val); //转换开头字母大写
            val = val.replace(/\\n/g, ' ');
            val = val.replace(/-/g, ' ');
            val = val.replace(/\s+/g, ''); //去掉空格
            val = val.replace(/,/g, '');
            val = val.replace(/\./g, '');
            val = 'Serve' + val;
            DownloadFunction(val);
          } else if (currentId === 'translateBaidu7') {
            val = startWithUppercase(val); //转换开头字母大写
            val = val.replace(/\\n/g, ' ');
            val = val.replace(/-/g, ' ');
            val = val.replace(/\s+/g, ''); //去掉空格
            val = val.replace(/,/g, '');
            val = val.replace(/\./g, '');
            val = val + 'Handler';
            DownloadFunction(val);
          } else if (currentId === 'translateBaidu3') {
            // 下划线
            val = startWithLowercase(val); //转换开头字母小写
            val = val.replace(/-/g, ' ');
            val = val.replace(/\s+/g, '_');
            val = val.substring(0, 1).toLowerCase() + val.substring(1);
            val = val.replace(/,/g, '');
            val = val.replace(/\./g, '');

            DownloadFunction(val);
          } else if (currentId === 'translateBaidu4') {
            // 中划线
            val = startWithLowercase(val); //转换开头字母小写
            val = val.replace(/-/g, ' ');
            val = val.replace(/\s+/g, '-');
            val = val.substring(0, 1).toLowerCase() + val.substring(1);
            val = val.replace(/,/g, '');
            val = val.replace(/\./g, '');

            DownloadFunction(val);
          }
        } catch (e) {
          alert(`插件运行有误，请联系【boxue】先生解决！`);
          console.log(e.message);
        }
      }
    });
  });
}
